﻿using System;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;

namespace LeanEngine.Reflection
{
    public static class ReflectionExtensions
    {
        public static object FastInvoke(this MethodInfo methodInfo, object instance, params object[] parameters)
        {
            return ReflectionCaches.MethodInvokerCache.GetItem(methodInfo).Invoke(instance, parameters);
        }

        public static void FastSetValue(this PropertyInfo propertyInfo, object instance, object value)
        {
            ReflectionCaches.PropertyAccessorCache.GetItem(propertyInfo).SetValue(instance, value);
        }

        public static object FastGetValue(this PropertyInfo propertyInfo, object instance)
        {
            return ReflectionCaches.PropertyAccessorCache.GetItem(propertyInfo).GetValue(instance);
        }

        public static object FastGetValue(this FieldInfo fieldInfo, object instance)
        {
            return ReflectionCaches.FieldAccessorCache.GetItem(fieldInfo).GetValue(instance);
        }

        public static object FastInvoke(this ConstructorInfo constructorInfo, params object[] parameters)
        {
            return ReflectionCaches.ConstructorInvokerCache.GetItem(constructorInfo).Invoke(parameters);
        }

        public static object FastCreateInstance(this Type classType, params object[] parameters)
        {
            return ReflectionCaches.EmitConstructorInvokerCache.GetItem(classType).Invoke(parameters);
        }
    }
}
